package com.wzq.controller.pc;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzq.controller.admin.GetSome;
import com.wzq.dao.pojo.Code;
import com.wzq.dao.pojo.Message;
import com.wzq.dao.pojo.Result;
import com.wzq.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @![](D:/STUDY/项目/视觉稿/A.04我的页面/我的首页.png)since 2022-08-31
 */
@RestController
@RequestMapping("/PC/message")
public class PCMessageController extends GetSome {

    @Autowired
    private IMessageService iMessageService;

    /**
     * 添加消息记录
     * yzc
     * (管理员，用户，默认系统)
     * @param message
     * @return
     */
    @PostMapping()
    public Result save(@RequestBody Message message ) {
        boolean flag =false ;
        //是否为管理员
        System.out.println(message);
        if (message.getName()!=null && "管理员".equals(message.getName())) {
            //给所有用户发消息
            for (int i : getAllW_id()){
                Message message1 = new Message();
                message1.setName("管理员");
                message1.setP_id(message.getP_id());
                message1.setW_id_down(i);
                iMessageService.save(message1);
            }
            return new Result(Code.SAVE_OK,flag);
        }
        //  用户发出
        if (message.getW_id()!=null && message.getW_id_down()!=null){
            flag=true;
            message.setName( getW_name(message.getW_id()) );
        }
        iMessageService.save(message);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }

    /**
     * 邀请成员
     * @param list
     * @param w_id
     * @param p_id
     * @return
     */
    @PostMapping("/{w_id}/wx/{p_id}/type/{type}")
    public Result wxsave(@RequestBody List<Integer> list,@PathVariable Integer w_id,@PathVariable Integer p_id,@PathVariable Integer type) {
        //  用户发出
        for (Integer a : list ) {
            Message message = new Message();
            message.setW_id(w_id);
            message.setP_id(p_id);
            message.setName(getW_name(w_id));
            message.setW_id_down(a);
            message.setType(type);
            iMessageService.save(message);
        }
        return new Result(Code.SAVE_OK,true);
    }

//    @PostMapping("ApplyMessage")
//    public Result ApplyMessage()

    //   修改
    @PutMapping("/{id}/")
    public Result update(@PathVariable Integer id) {
        Message message = iMessageService.getById(id);
        message.setIsdelete(1);
        boolean flag = iMessageService.updateById(message);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iMessageService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }


    /**
     * id查询未读消息记录
     * @return
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper();
        lqw.eq(Message::getW_id_down,id).eq(Message::getIsdelete,0).eq(Message::getIsread,0);
        List<Message> messages = new ArrayList<>();
        for (Message m : iMessageService.list(lqw) ) {
            if (m.getType()==0)     m.setP_type(getT_type(iProjectService.getById(m.getP_id()).getType()));
            if (m.getP_id()!=null)
                m.setP_name( super.getP_name(m.getP_id()) );
                messages.add(m);
        }
        Integer code = messages != null ? Code.GET_OK : Code.GET_ERR;
        String msg = messages != null ? "" : "数据查询失败，请重试！";
        return new Result(code,messages,msg);
    }


    /**
     * id查询已读消息记录
     * @return
     */
    @GetMapping("/isread/{id}")
    public Result getByIdIsRead(@PathVariable Integer id) {
        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper();
        lqw.eq(Message::getW_id_down,id).eq(Message::getIsdelete,0).eq(Message::getIsread,1);
        List<Message> message = iMessageService.list(lqw);
        List<Message> messages = new ArrayList<>();
        for (Message m : message ) {
            if (m.getType()==0)     m.setP_type(getT_type(iProjectService.getById(m.getP_id()).getType()));
            if (m.getP_id()!=null)

                m.setP_name( super.getP_name(m.getP_id()) );
            messages.add(m);
        }
        Integer code = message != null ? Code.GET_OK : Code.GET_ERR;
        String msg = message != null ? "" : "数据查询失败，请重试！";
        return new Result(code,messages,msg);
    }

    /**
     * id查询未读消息记录数量
     * @return
     */
    @GetMapping("/unread/{id}")
    public Result getByReadCount(@PathVariable Integer id) {
        LambdaQueryWrapper<Message> lqw = new LambdaQueryWrapper();
        lqw.eq(Message::getW_id_down,id).eq(Message::getIsdelete,0).eq(Message::getIsread,0);
        int count = iMessageService.count(lqw);
        return new Result(Code.GET_OK,count);
    }
    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Message> list = iMessageService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }






}
